/*
 * 描          述:  <描述>
 * 修  改   人:  Administrator
 * 修改时间:  2019年5月29日
 * <修改描述:>
 */
package com.tx.core.generator.util;

import com.tx.core.exceptions.util.AssertUtils;
import com.tx.core.generator.model.EntityProperty;
import com.tx.core.generator.model.impl.JPAEntityProperty;
import com.tx.core.util.TableInfoUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.v3.oas.annotations.media.Schema;

import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 生成工具类<br/>
 * <功能详细描述>
 *
 * @author Administrator
 * @version [版本号, 2019年5月29日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class JPAGeneratorUtils {

    /**
     * 判断传入的类是否为基础数据枚举类型<br/>
     *
     * @return
     */
    public static boolean isBasicdataEnum(Class<?> type) {
        Class<?> basicDataEnumClazz = null;
        try {
            basicDataEnumClazz = Class.forName("com.tx.component.basicdata.model.BasicDataEnum");
        } catch (ClassNotFoundException e) {
            //doNothing
        }
        if (basicDataEnumClazz == null) {
            return false;
        } else {
            return type.isEnum() && basicDataEnumClazz.isAssignableFrom(type);
        }
    }

    /**
     * 判断是否为基础数据类型
     *
     * @param type
     * @return
     */
    public static boolean isBasicdataType(Class<?> type) {
        Class<?> basicDataClazz = null;
        try {
            basicDataClazz = Class.forName("com.tx.component.basicdata.model.BasicData");
        } catch (ClassNotFoundException e) {
            //doNothing
        }
        if (basicDataClazz == null) {
            return false;
        } else {
            return basicDataClazz.isAssignableFrom(type);
        }
    }

    /**
     * 判断是否为树结构基础数据
     *
     * @param type
     * @return
     */
    public static boolean isTreeBasicdataType(Class<?> type) {
        Class<?> treeBasicDataClazz = null;
        try {
            treeBasicDataClazz = Class.forName("com.tx.component.basicdata.model.TreeBasicData");
        } catch (ClassNotFoundException e) {
            //doNothing
        }
        if (treeBasicDataClazz == null) {
            return false;
        } else {
            return treeBasicDataClazz.isAssignableFrom(type);
        }
    }

    /**
     * 解析实体对象的注释<br/>
     * <功能详细描述>
     *
     * @param beanType
     * @return [参数说明]
     * @return String [返回类型说明]
     * @throws throws [异常类型] [异常说明]
     * @see [类、类#方法、类#成员]
     */
    public static String parseEntityComment(Class<?> beanType) {
        AssertUtils.notNull(beanType, "beanType is null.");
        String entityComment = beanType.getSimpleName();
        if (beanType.isAnnotationPresent(ApiModel.class)) {
            ApiModel anno = beanType.getAnnotation(ApiModel.class);
            if (StringUtils.isNotBlank(anno.value())) {
                entityComment = anno.value();
                return entityComment;
            } else if (StringUtils.isNotBlank(anno.description())) {
                entityComment = anno.description();
                return entityComment;
            }
        }
        if (beanType.isAnnotationPresent(Schema.class)) {
        	Schema anno = beanType.getAnnotation(Schema.class);
            if (StringUtils.isNotBlank(anno.description())) {
                entityComment = anno.description();
                return entityComment;
            }
        }
        return entityComment;
    }

    /**
     * 解析表字段<br/>
     * <功能详细描述>
     *
     * @param beanType
     * @return [参数说明]
     * @return List<JPAProperty> [返回类型说明]
     * @throws throws [异常类型] [异常说明]
     * @see [类、类#方法、类#成员]
     */
    public static List<EntityProperty> parseEntityPropertyList(
            Class<?> beanType) {
        AssertUtils.notNull(beanType, "beanType is null.");

        //解析jpa属性
        List<TableInfoUtils.TableColumnInfo> columns = TableInfoUtils.parseTableColumns(beanType);
        List<EntityProperty> propertyList = columns.stream()
                .map(column -> {
                    return new JPAEntityProperty(column);
                })
                .collect(Collectors.toList());
        return propertyList;
    }
}
